
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package doghost.controllers;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.RollbackException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import doghost.DoghostApp;
import doghost.editors.ServicoEditor;
import doghost.entities.Servico;

/**
 *
 * @author duo
 */
public class ServicoController {
    protected Servico servico;
    protected boolean operationOk;

    public Servico getServico() {
        return servico;
    }

    public void setServico(Servico servico) {
        this.servico = servico;
    }

    /**
     * Get the value of operationOk
     *
     * @return the value of operationOk
     */
    public boolean isOperationOk() {
        return operationOk;
    }

    /**
     * Set the value of operationOk
     *
     * @param operationOk new value of operationOk
     */
    public void setOperationOk(boolean operationOk) {
        this.operationOk = operationOk;
    }

    public void newServico(EntityManager entityManager, List list) {
        // create a transient object
        this.setOperationOk(false);
        setServico(new Servico());
        JFrame mainFrame = DoghostApp.getApplication().getMainFrame();
        ServicoEditor servicoEditor = new ServicoEditor(mainFrame, true);
        servicoEditor.setLocationRelativeTo(mainFrame);
        servicoEditor.setCurrentRecord(getServico());
        servicoEditor.setVisible(true);
        if (servicoEditor.isSaveFired()) {
            try {
                entityManager.getTransaction().begin();
                entityManager.persist(getServico());
                entityManager.getTransaction().commit();
                this.setOperationOk(true);
                if(list != null) {
                    list.add(getServico());
                }
                Logger.getLogger(this.getClass().getName()).
                        log(Level.INFO, "Servico adicionado(a) com sucesso: " +
                        getServico());
            }
            catch (Exception e) {
                Logger.getLogger(this.getClass().getName()).
                        log(Level.SEVERE, "Erro adicionando Servico: " + e);
            }
        }
    }

    public void editServico(EntityManager entityManager, List list, int index) {
        this.setOperationOk(false);
        entityManager.getTransaction().begin();
        JFrame mainFrame = DoghostApp.getApplication().getMainFrame();
        ServicoEditor servicoEditor = new ServicoEditor(mainFrame, true);
        servicoEditor.setLocationRelativeTo(mainFrame);
        servicoEditor.setCurrentRecord(getServico());
        servicoEditor.setVisible(true);
        if (servicoEditor.isSaveFired()) {
            try {
                entityManager.merge(getServico());
                entityManager.getTransaction().commit();
                this.setOperationOk(true);
                Logger.getLogger(this.getClass().getName()).
                    log(Level.INFO, "Servico Editado(a) com sucesso: " +
                    getServico());
            } catch (RollbackException rex) {
                // refresh servico in list
                Logger.getLogger(this.getClass().getName()).
                    log(Level.SEVERE, "Servico erro de edição (rollback) : " +
                    rex);
                Servico originalServico = (Servico)
                        entityManager.find(getServico().getClass(),
                        getServico().getId());
                if(list != null) {
                    list.remove(getServico());
                    list.add(index, originalServico);
                }
            } catch (Exception e) {
                Logger.getLogger(this.getClass().getName()).
                        log(Level.SEVERE, "Servico erro de edição: " + e);
                if (entityManager.getTransaction().isActive()) {
                    try {
                        entityManager.getTransaction().rollback();
                    } catch (Exception erb) {
                        Logger.getLogger(this.getClass().getName()).
                                log(Level.SEVERE, "Erro abandonando edição de servico, " +
                                "transação ativa: " + erb);
                    }
                }
            }
        } else {
            try {
                if (entityManager.getTransaction().isActive()) {
                    entityManager.refresh(getServico());
                    entityManager.getTransaction().commit();
                }
                // refresh servico in list
                Servico originalServico = (Servico)
                        entityManager.find(getServico().getClass(),
                        getServico().getId());
                if(list != null) {
                    list.remove(getServico());
                    list.add(index, originalServico);
                }
                Logger.getLogger(this.getClass().getName()).
                    log(Level.INFO, " Edição de Servico cancelada com sucesso " +
                    "servico: " + getServico());
            } catch (RollbackException rex) {
                Logger.getLogger(this.getClass().getName()).
                    log(Level.SEVERE, "Erro cancelando edição de servico " +
                    "(rollback) : " + rex);
                if (entityManager.getTransaction().isActive()) {
                    try {
                        entityManager.getTransaction().rollback();
                    } catch (Exception erb) {
                        Logger.getLogger(this.getClass().getName()).
                                log(Level.SEVERE, "Erro abandonando eliminação de servico. " +
                                "transação ativa: " + erb);
                    }
                }
            }
        }
    }

    public void deleteServico(EntityManager entityManager, List list, int index) {
        this.setOperationOk(false);
        Object[] options = {"Sim", "Não"};
        int n = JOptionPane.showOptionDialog(null, "Remove a linha ?", "Aviso",
                JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, options, null);
        if (n ==JOptionPane.YES_OPTION) {
            try {
                entityManager.getTransaction().begin();
                entityManager.remove(getServico());
                entityManager.getTransaction().commit();
                this.setOperationOk(true);
                if(list != null) {
                    list.remove(getServico());
                }
            } catch (Exception e) {
                Logger.getLogger(this.getClass().getName()).
                    log(Level.SEVERE, "Erro eliminando servico: " + e);
            }
        }
    }


}

